//
//  E7AnimationSprite.cpp
//  EndlessBattle
//
//  Created by cc on 12-4-30.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7AnimationSprite.h"

CE7AnimationSprite::CE7AnimationSprite()
{
}

CE7AnimationSprite::~CE7AnimationSprite()
{
}

void CE7AnimationSprite::Init(CCNode* pItem, eAnimationIDs index)
{
    m_eID = index ;

    switch (m_eID)
    {
        case ANI_SPRITE_TRANSFORM:
        {
            m_bIsLoop = true ;
            m_nFrameMax = 3 ;
            m_fInterval = 0.1f ;
            m_pFrames = new CE7FrameInfo[m_nFrameMax] ;
            InitWithTransform(0, 2, pItem, pItem->getPosition(), CCPointMake(pItem->getPosition().x, pItem->getPosition().y+10)) ;
            InitWithTransform(2, 1, pItem, pItem->getPosition(), pItem->getPosition()) ;

        } break ;
        case ANI_SPRITE_SCALE:
        {
            m_bIsLoop = false ;
            m_nFrameMax = 7 ;
            m_fInterval = 0.1f ;
            m_pFrames = new CE7FrameInfo[m_nFrameMax] ;
            InitWithScale(0, 5, pItem, 0.0f, 1.5f) ;
            InitWithScale(5, 2, pItem, 1.3f, 1.0f) ;
        } break ;
        case ANI_SPRITE_ALPHA:
        {
            m_bIsLoop = false ;
            m_nFrameMax = 10 ;
            m_fInterval = 0.1f ;
            m_pFrames = new CE7FrameInfo[m_nFrameMax] ;
            InitWithAlpha(0, 10, pItem, 0, 255) ;
        } break ;
        case ANI_SPRITE_ALPHA_LOOP:
        {
            m_bIsLoop = true ;
            m_nFrameMax = 20 ;
            m_fInterval = 0.1f ;
            m_pFrames = new CE7FrameInfo[m_nFrameMax] ;
            InitWithAlpha(0, 10, pItem, 0, 255) ;
            InitWithAlpha(10, 10, pItem, 255, 0) ;
        } ;
        default:
            break;
    }
}

void CE7AnimationSprite::InitWithTransform(int startindex, int framecount, CCNode* pItem, CCPoint start_pos, CCPoint end_pos)
{
    CCPoint offset = CCPointMake((end_pos.x-start_pos.x)/framecount, (end_pos.y-start_pos.y)/framecount) ;
    for (int i = 0; i < framecount; ++i)
    {
        m_pFrames[startindex+i].SetInfo(pItem,
                             CCPointMake(start_pos.x+offset.x*i, start_pos.y+offset.y*i),
                             pItem->getRotation(),
                             pItem->getScale(),
                             ((CCSprite*)pItem)->getOpacity()) ;
    }
}

void CE7AnimationSprite::InitWithRotation(int startindex, int framecount, CCNode* pItem, float start_angle, float end_angle)
{
    float offset_angle = (end_angle-start_angle)/(framecount-1) ;
    printf("offset angle === [%f]\n", offset_angle) ;
    for (int i = 0; i < framecount; ++i)
    {
        m_pFrames[startindex+i].SetInfo(pItem,
                                        pItem->getPosition(),
                                        start_angle+offset_angle*i,
                                        pItem->getScale(),
                                        ((CCSprite*)pItem)->getOpacity()) ;
    }
}

void CE7AnimationSprite::InitWithScale(int startindex, int framecount, CCNode* pItem, float start_scale, float end_scale)
{
    float offset_scale = (end_scale-start_scale)/(framecount-1) ;
    printf("offset scale === [%f]\n", offset_scale) ;
    for (int i = 0; i < framecount; ++i)
    {
        m_pFrames[startindex+i].SetInfo(pItem,
                                        pItem->getPosition(),
                                        pItem->getRotation(),
                                        start_scale+offset_scale*i,
                                        ((CCSprite*)pItem)->getOpacity()) ;
    }
}

void CE7AnimationSprite::InitWithAlpha(int startindex, int framecount, CCNode* pItem, int start_alpha, int end_alpha)
{
    GLbyte offset_alpha = (end_alpha-start_alpha)/(framecount-1) ;
    printf("offset alpha === [%d]\n", offset_alpha) ;
    for (int i = 0; i < framecount; ++i)
    {
        m_pFrames[startindex+i].SetInfo(pItem,
                                        pItem->getPosition(),
                                        pItem->getRotation(),
                                        pItem->getScale(),
                                        start_alpha+offset_alpha*i) ;
    }
}















